/**
 * Scorm本地实现方法
 * @author Willi WH Yang
 */
package ywh.utils.scorm {
	import ywh.utils.TimeFormat;

	import flash.net.SharedObject;

	public class SO_Scorm_Wrapper implements IScormAPI {
		private var sharedObject : SharedObject;

		public function SO_Scorm_Wrapper(soName : String) {
			sharedObject = SharedObject.getLocal(soName);
			//sharedObject.clear();
			setValue("cmi.core.session_time", "00:00:00.00");
		}

		/* INTERFACE IScormAPI */
		public function connect() : Boolean {
			return true;
		}

		public function disconnect() : Boolean {
			return true;
		}

		public function getValue(param : String) : String {
			return parseCMIParamToArray(param)["data"];
		}

		public function setValue(parameter : String, value : String) : Boolean {
			if (parameter == "cmi.core.session_time" && value != "00:00:00.00") {
				var ptotal : String = "cmi.core.total_time";
				var ctime : Number = TimeFormat.hhmmss2ss(getValue(ptotal));
				ctime = isNaN(ctime) ? 0 : ctime;
				ctime = ctime - TimeFormat.hhmmss2ss(getValue(parameter)) + TimeFormat.hhmmss2ss(value);
				parseCMIParamToArray(ptotal)["data"] =TimeFormat.ss2hhmmss(ctime, true);
			}
			parseCMIParamToArray(parameter)["data"] =value;
			// save();
			return true;
		}

		public function save() : Boolean {
			sharedObject.flush();
			return true;
		}

		private function parseCMIParamToArray(param : String) : Object {
			var a : Array = param.split(".");
			var o : Object = sharedObject.data;
			while (a.length > 0) {
				var s : String = String(a.shift());
				if (!o[s]) {
					o[s] = {};
				}
				o = o[s];
			}
			return o;
		}
	}
}